package com.visionblue.business.ship.print

import com.visionblue.business.ship.model.bean.local.PrintInfo
import com.visionblue.business.ship.util.GBKHexUtil
import com.visionblue.business.ship.util.MLog
import java.nio.charset.Charset
import java.util.*

/**
 * @author: xlh
 * @date: 2020-06-19 1:47 PM
 * @desc:
 */
class E9LPrintFormat : PrintFormat {

    private val mBytes = mutableListOf<Byte>()
    private val yPs = arrayOf(0x2f, 0x4B.toByte(), 0x68.toByte(), 0x83.toByte(), 0x9C.toByte())


    private fun writeStart() {
        //    {0x1A,0X5B,0X01,0X00,0X00,0X00,0X00,0X80,0X01,0XFA,0X00,0X00}
        val startTag = listOf<Byte>(0x1B, 0x40, 0x1A, 0x5b, 0x01)
        val startPoint = listOf<Byte>(0, 0, 0, 0)
        val range = listOf(0x80.toByte(), 0x01, 0xFA.toByte(), 0x00.toByte())
        mBytes.addAll(startTag)
        mBytes.addAll(startPoint)
        mBytes.addAll(range)
        mBytes.add(DIRECTIVE_END)
    }

    private fun writeEnd() {
        mBytes.addAll(listOf(0x1A, 0x5D, 0x00, 0x1A, 0x4F, 0x00))
    }

    private fun writeQrCode(qrCode: String) {
        val start = listOf<Byte>(0x1A, 0x31, 0x00, 0x05, 0x02)
        val startPoint = listOf(0xE0.toByte(), 0x00, 0x15, 0x00)
        val codeSize = listOf<Byte>(0x04)
        val rotate = listOf<Byte>(0x00)

        mBytes.addAll(start)
        mBytes.addAll(startPoint)
        mBytes.addAll(codeSize)
        mBytes.addAll(rotate)
        val toTypedArray = qrCode.toByteArray().toList()
//        MLog.e("toTypedArray1:${toTypedArray.contentToString()}" )
//        MLog.e("toTypedArray: ${GBKHexUtil.bytes2HexString(qrCode.toByteArray())}")
        mBytes.addAll(toTypedArray)
        mBytes.add(DIRECTIVE_END)
    }

    private fun writeText(startPoint: List<Byte>, content: String, vararg others: Byte) {
        val start = listOf<Byte>(0x1A, 0x54, 0x01)
        val textSize = listOf(0x10, 0x00.toByte(), 0x00, 0x11)
        mBytes.addAll(start)
        mBytes.addAll(startPoint)
        mBytes.addAll(textSize)
        mBytes.addAll(content.toGBKHex())
        if (others.isNotEmpty()) {
            mBytes.addAll(others.toList())
        }
        mBytes.add(DIRECTIVE_END)
    }

    override fun toBytes(printInfo: PrintInfo): ByteArray {
        mBytes.clear()
        writeStart()
        writeText(listOf(0x00, 0x00, 0x15, 0x00), "被接收船：${printInfo.fromShip}")
        var index = 0

        for (i in printInfo.wasteInfoMap.indices) {
            writeText(listOf(0x00, 0x00, yPs[index++], 0x00), printInfo.wasteInfoMap[i])
            if (index >= 3) {
                break
            }
        }
//        printInfo.wasteInfoMap.forEach {
//            writeText(listOf(0x00, 0x00, yPs[index++], 0x00), it)
//        }
//        writeText(listOf(0x00, 0x00, 0x2f, 0x00), "含油污水：78.98m", *("m³".toByteArray()))
//        writeText(listOf(0x00, 0x00, 0x4B.toByte(), 0x00), "生活垃圾：58.21kg")
//        writeText(listOf(0x00, 0x00, 0x68.toByte(), 0x00), "生活污水：0.81m")
        writeText(listOf(0x00, 0x00, yPs[index++], 0x00), "接收船：${printInfo.toShip}")
        writeText(listOf(0x00, 0x00, yPs[index], 0x00), "日期：${printInfo.date}")
        if (!printInfo.pdfUrl.isNullOrEmpty()) {
            writeQrCode(printInfo.pdfUrl)
        }
        writeEnd()
        return mBytes.toByteArray()
    }


    companion object {
        private const val DIRECTIVE_END = 0x00.toByte()
    }

}